package com.qdxwx.manage.controller.post;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.manager.PostService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.manager.Post;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * 岗位信息操作处理
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/post")
public class PostController extends BaseController {

    @Autowired
    private PostService postService;

    @Permissions("post:list")
    @GetMapping("/list.html")
    public String list() {
        return "post/post-list";
    }

    @Permissions("post:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        String searchKey = TKString.trim(request.getParameter("searchKey"));
        Post post = new Post();
        //post.setPostCode(TKString.queryKey(request.getParameter("postCode")));
        if (searchKey.length() > 0) {
            post.setPostName(searchKey);
        }
        String isDisabled = request.getParameter("isDisabled");
        if (TKString.isNotEmpty(isDisabled)) {
            post.setIsDisabled(TKConvert.toInteger(isDisabled) <= 0 ? 0 : 1);
        }
        List<Post> list = postService.getList(post, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(postService.count(post));
        return rspData;
    }

    @Permissions("post:remove")
    @Log(title = "删除岗位", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        Integer[] postIds = TKConvert.toIntArray(request.getParameter("ids"));
        if (postIds.length < 1) return error("至少选择一条");
        try {
            return toAjax(postService.deleteByIds(postIds));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 新增岗位
     */
    @GetMapping("/add.html")
    public String add() {
        return "post/post-add";
    }

    /**
     * 新增保存岗位
     */
    @Permissions("post:add")
    @Log(title = "添加岗位", businessType = BusinessType.INSERT)
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult add(HttpServletRequest request) {
        String postName = request.getParameter("postName");
        if (TKString.isEmpty(postName)) return error("请输入岗位名称");
        if (postService.checkPostName(postName)) return error("岗位名称已经存在");
        String postCode = request.getParameter("postCode");
        if (TKString.isEmpty(postCode)) return error("请输入岗位编码");
        if (postService.checkPostCode(postCode)) return error("岗位编码已经存在");
        Post post = new Post();
        post.setPostName(postName);
        post.setPostCode(postCode);
        post.setSort(TKConvert.toInteger(request.getParameter("sort")));
        post.setIsDisabled(TKConvert.toInteger(request.getParameter("isDisabled")) <= 0 ? 0 : 1);
        post.setRemark(request.getParameter("remark"));
        post.setCreateBy(SessionUtils.getLoginName());

        return toAjax(postService.insert(post));
    }

    /**
     * 修改岗位
     */
    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int postId = TKConvert.toInteger(request.getParameter("id"));
        if (postId < 1) return error404;
        Post post = postService.get(postId);
        if (post == null) return error404;
        mmap.put("post", post);
        return "post/post-edit";
    }

    /**
     * 修改保存岗位
     */
    @Permissions("post:edit")
    @Log(title = "编辑岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult editSave(HttpServletRequest request) {
        int postId = TKConvert.toInteger(request.getParameter("postId"));
        if (postId < 1) return error("找不到id参数");
        Post post = postService.get(postId);
        if (post == null) return error("找不到指定修改数据");
        post.setSort(TKConvert.toInteger(request.getParameter("sort")));
        post.setRemark(request.getParameter("remark"));
        post.setIsDisabled(TKConvert.toInteger(request.getParameter("isDisabled")) <= 0 ? 0 : 1);
        post.setUpdateBy(SessionUtils.getLoginName());
        return toAjax(postService.update(post));
    }

    /**
     * 岗位状态修改
     */
    @Log(title = "岗位状态", businessType = BusinessType.UPDATE)
    @Permissions("post:edit")
    @PostMapping("/status.json")
    @ResponseBody
    public AjaxResult status(HttpServletRequest request) {
        int postId = TKConvert.toInteger(request.getParameter("postId"));
        if (postId < 1) return error("postId参数异常，value=" + postId);
        Post post = postService.get(postId);
        if (post == null) return error("找不到指定数据");
        int status = TKConvert.toInteger(request.getParameter("status"));
        post.setIsDisabled(status == 0 ? 0 : 1);
        return toAjax(postService.updateDisabled(post));
    }

}
